package com.prim.threadcoreknowledge.stopthread;

/**
 * 注意
 * static boolean interrupted() 清除中断状态
 * boolean isInterrupted() 不会清除中断状态
 * Thread.interrupted() 目标对象是当前线程
 * 需要注意Thread类的interrupted()静态方法中是获取currentThread 调用中断方法
 */
public class RightWayInterrupted {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                for (; ; ) {
                }
            }
        });
        thread.start();
        //设置中断标志
        thread.interrupt();
        //获取中断标志
        System.out.println("获取中断标志不会重置中断标志isInterrupted:" + thread.isInterrupted());//true
        //获取中断标志并重置 thread.interrupted() 静态方法但是获取的是当前线程而当前的线程是主线程，主线程并没有中断 所以返回false
        System.out.println("获取中断标志并重置中断标志 isInterrupted:" + thread.interrupted());//false
        //获取中断标志并重置 获取当前的线程也就是主线程 返回false
        System.out.println("Thread.interrupted() 获取中断标志并重置中断标志 isInterrupted:" + Thread.interrupted());//false
        //重新获取中断标志 前两个方法并不会清除状态 所以还是返回true
        System.out.println("isInterrupt:" + thread.isInterrupted());//true
        thread.join();
        System.out.println("Main Thread is over");
    }
}
